<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1>jquery的ajax请求</h1>
    <button id="btn">发起请求</button>
</body>

</html>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>


    btn.onclick = function () {
        getajax({ 
            // type:"get",
            // url:"http://localhost:3000/students",
            data:{
                username:'zs'
            },
            success(res){
                console.log(res);
            }
       })
    }
   


    function getajax( json) {

       let {type="get",url="http://localhost:3000/index",data={},success=function(){}}=json
        // console.log(type,url,data,success);
       

        let xhr = new XMLHttpRequest()
        xhr.open(type, url)
        xhr.setRequestHeader("Content-type", "application/json")
        xhr.send(JSON.stringify(data))

        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {

                console.log(xhr.responseText);
                // let results = JSON.parse(xhr.responseText)

                // 处理results
                // 1、
                // 2、
                // 3、
                // 4、

                success(results)

            }
        }
    }




</script>